package paylayer

import (
	//"strconv"
	"encoding/json"
	"dhfshop/service/nsqcli"
	"github.com/astaxie/beego"
)

func ComPayNotify(payNotify *CommonPayNotify) (err error) {
	 var (
		successRspJson []byte
		rspFailJson    []byte
	)
	if payNotify.ResultCode == "SUCCESS" {
	    successRsp := struct {
			AppId         string  `json:"app_id"`         //小程序ID
			MchId         string  `json:"mch_id"`         //商户号
			OpenId        string  `json:"open_id"`        //用户标识
			PayWayId      int     `json:"pay_way_id"`     //支付商编号
			TotalFee      float32 `json:"total_fee"`      //支付金额，单位元
			FeeType       string  `json:"fee_type"`       //货币种类, 默认人民币：CNY
			TransactionId string  `json:"transaction_id"` //微信支付订单号
			OrderId       string  `json:"order_id"`       //商户订单号
			Kind          string  `json:"kind"`           //业务类型
			TimeEnd       string  `json:"time_end"`       //支付完成时间，格式为yyyyMMddHHmmss
			ResultCode    string  `json:"result_code"`            //SUCCESS/FAIL，业务结果
			ErrCode       string  `json:"err_code,omitempty"`     //错误代码
			ErrCodeDes    string  `json:"err_code_des,omitempty"` //错误代码描述
			CouponFee     float32 `json:"coupon_fee"`             //代金券或立减优惠金额，单位元
		}{AppId:payNotify.AppId,MchId:payNotify.MchId,OpenId:payNotify.OpenId,FeeType:payNotify.FeeType}
		successRsp.PayWayId = payNotify.PayWayId
		successRsp.TransactionId = payNotify.TransactionId 
		successRsp.OrderId = payNotify.OrderId
        successRsp.Kind = payNotify.Kind
        successRsp.TimeEnd = payNotify.TimeEnd
        successRsp.ResultCode = payNotify.ResultCode		
		successRsp.ErrCode = payNotify.ErrCode
		successRsp.ErrCodeDes = payNotify.ErrCodeDes  
		successRsp.TotalFee = payNotify.TotalFee
		successRsp.CouponFee = payNotify.CouponFee
		successRspJson, err = json.Marshal(&successRsp)
		if err != nil {
			beego.Debug(err.Error())
			return
		}
		address := beego.AppConfig.String("NsqUrl")
		topic := "commonpay"
		if err = nsqcli.StartProducer(address, topic, successRspJson); err != nil {
			beego.Debug("StartProducer err:", err.Error())
		}
	} else {
	    failRsp := struct {
			ReturnCode string  `json:"return_code"`          //返回状态码
			ReturnMsg  string  `json:"return_msg,omitempty"` //返回信息          
		}{ReturnCode:payNotify.ResultCode, ReturnMsg:payNotify.ErrCode}
		rspFailJson, err = json.Marshal(&failRsp)
		if err != nil {
			beego.Debug(err.Error())
			return
		}
		address := beego.AppConfig.String("NsqUrl")
		topic := "commonpay"
		if err = nsqcli.StartProducer(address, topic, rspFailJson); err != nil {
			beego.Debug("StartProducer err:", err.Error())
		}
	}
	return
}
